package com.pitb.citizenreporting.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.pitb.citizenreporting.App;
import com.pitb.citizenreporting.ReportModel;

public class HakathonDb {
	public static String LOCK = "dblock";
	private DBTableHelper dbHelper = new DBTableHelper(App.getInstance()
			.getApplicationContext());

	public SQLiteDatabase getdb() {
		SQLiteDatabase db = this.dbHelper.getWritableDatabase();
		db.setLockingEnabled(true);
		return db;
	}

	public long insertCompalint(String numberarg, String addressarg,
			String problemarg, String provincearg, String cityarg,
			String areaarg, String typearg, String status, String tracking_id) {
		ContentValues values = new ContentValues();
		values.put(DBInterfaces.PROBLEM.PROBLEM, problemarg);
		values.put(DBInterfaces.PROBLEM.STREET, addressarg);
		values.put(DBInterfaces.PROBLEM.AREA, areaarg);
		values.put(DBInterfaces.PROBLEM.CITY, cityarg);
		values.put(DBInterfaces.PROBLEM.PROVINCE, provincearg);
		values.put(DBInterfaces.PROBLEM.CONTACT, numberarg);
		values.put(DBInterfaces.PROBLEM.TYPE, typearg);
		values.put(DBInterfaces.PROBLEM.STATUS, status);
		values.put(DBInterfaces.PROBLEM.DATE, App.getDateTime());
		values.put(DBInterfaces.PROBLEM.TRACKING_ID, tracking_id);
		synchronized (LOCK) {
			SQLiteDatabase db = getdb();
			long id = db.insert(DBInterfaces.Tables.PROBLEM, null, values);
			db.close();
			Log.i("Reporting Ap","Inserted succesfully , ID is " + id);
			return id;
		}
	}

	
//
//	public boolean isUrlExist(String paramString) {
//		boolean isExist = false;
//		synchronized (LOCK) {
//			SQLiteDatabase db = getdb();
//			Cursor cursor = db.query(DBInterfaces.Tables.PROBLEM, null,
//					DBInterfaces.PROBLEM.URL + "='" + paramString + "'", null,
//					null, null, null);
//			if (cursor.getCount() > 0)
//				isExist = true;
//			if ((cursor != null) && (!cursor.isClosed()))
//				cursor.close();
//			db.close();
//			return isExist;
//		}
//	}
//
//	public void updateEntry(long paramLong, String paramString1,
//			String paramString2) {
//		ContentValues values = new ContentValues();
//		values.put(DBInterfaces.PROBLEM.URL, paramString1);
//		values.put(DBInterfaces.PROBLEM.PATH, paramString2);
//		values.put(DBInterfaces.PROBLEM.TIMESTAMP,
//				Long.valueOf(System.currentTimeMillis()));
//		synchronized (LOCK) {
//			SQLiteDatabase db = getdb();
//			db.update(DBInterfaces.Tables.PROBLEM, values,
//					DBInterfaces.PROBLEM.ID + "=" + paramLong, null);
//			db.close();
//		}
//	}

//	public List<ReportModel> getEntries(long range) {
//		List<ReportModel> List = new ArrayList<ReportModel>();
//		synchronized (LOCK) {
//			SQLiteDatabase db = getdb();
//			Cursor cursor = db.query(DBInterfaces.Tables.PROBLEM, null,
//					DBInterfaces.PROBLEM.EXPIRE + "<=" + range, null, null,
//					null, null);
//			if (cursor.moveToFirst()) {
//				do {
//					ReportModel entry = new ReportModel();
//					entry.id = cursor.getLong(cursor
//							.getColumnIndex(DBInterfaces.PROBLEM.ID));
//					entry.filename = cursor.getString(cursor
//							.getColumnIndex(DBInterfaces.PROBLEM.PATH));
//					List.add(entry);
//				} while (cursor.moveToNext());
//			}
//			if ((cursor != null) && (!cursor.isClosed()))
//				cursor.close();
//			db.close();
//			return List;
//		}
//	}

	public List<ReportModel> getEntries() {
		List<ReportModel> List = new ArrayList<ReportModel>();
		synchronized (LOCK) {
			SQLiteDatabase db = getdb();
			Cursor cursor = db.query(DBInterfaces.Tables.PROBLEM, null, null,
					null, null, null, null);
			if (cursor.moveToFirst()) {
				do {
					ReportModel entry = new ReportModel();
					entry.id = cursor.getLong(0);
					entry.problem = cursor.getString(1);
					entry.status = cursor.getString(2);
					entry.street= cursor.getString(3);
					entry.area= cursor.getString(4);
					entry.city= cursor.getString(5);
					entry.province = cursor.getString(6);
					entry.contact = cursor.getString(7);
					entry.date= cursor.getString(8);
					entry.traking_id= cursor.getString(9);
					entry.revisions= cursor.getLong(10);
					entry.comments = cursor.getString(11);
					List.add(entry);
				} while (cursor.moveToNext());
			}
			if ((cursor != null) && (!cursor.isClosed()))
				cursor.close();
			db.close();
			return List;
		}
	}

}
